/// <reference path="../../typings/weapp.d.ts" />
import { WeApp } from '../../common/common';
import { config } from '../../etc/config';
// import * as __ from 'underscore';
import httpClient from '../../common/http-client';
import wxService from '../../common/wxService';
import { MlcEnum } from '../../model/enum';
import { IUserData, IUser } from '../../model/user';

const app: WeApp = getApp() as WeApp;

interface UserIndexPagePageData {
	userInfo: IUser
}

interface UserIndexPage extends IPage {
}

class UserIndexPage {
	async getUserData() {
		wx.showNavigationBarLoading();
		const res = await httpClient.get<IUserData>(config.user.user_index);
		if (res.succeed) {
			this.setData({
				userData: res.value
			});
		} else {
			wxService.showToast(res.msg);
		}
		wx.stopPullDownRefresh();
		wx.hideNavigationBarLoading();
	}

	public data: UserIndexPagePageData = {
		userInfo: {}
	}

	public async onLoad() {
		const info = await app.getUserInfo();
		this.setData({
			'userInfo': info
		})
	}

	public onShow() {
		this.onPullDownRefresh();
	}

	onPullDownRefresh() {
		this.getUserData();
	};

	scanCodeEvent(e) {
		wxService.scanCode({ desc: '将二维码放入框内即可自动扫描' })
			.then((res: wx.ScanCodeResult) => {
				if (res.errMsg.indexOf('ok') > -1) {
					console.log('00000000000:' + res.result);
					httpClient.post(config.user.login_code, {
						code: res.result,
						userId: this.data.userInfo.id
					}).then(res => {
						if (!res.succeed) {
							wxService.showToast(res.msg);
						}
					})
				}
			})
	}

	async qrCodeEvent(){
		var userId =(wx.getStorageSync("userInfo") as IUser).id
	}


}

Page(new UserIndexPage());
